package com.chaos.eurekaproducer.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author liaopeng
 * @title: MqConsumerInterceptor
 * @projectName eureka-producer
 * @description:
 * @date 2021/12/10上午9:48
 */
@Component
@Aspect
public class MqConsumerInterceptor {

    @Pointcut("@annotation(org.springframework.amqp.rabbit.annotation.RabbitListener)")
    public void mqPointCut(){}

    @Before(value = "mqPointCut()")
    public void around(JoinPoint point) throws Throwable {
        Message message= (Message)point.getArgs()[0];
        String messageId = message.getMessageProperties().getMessageId();
        String queue = message.getMessageProperties().getConsumerQueue();
        String exchange = message.getMessageProperties().getReceivedExchange();
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        System.out.println("rabbitMq消息参数，messageId="+messageId+"，exchange="+exchange+"，queue="+queue+",routingKey="+routingKey);
        System.out.println("拦截rabbitmq消费参数："+new String(message.getBody()));
    }

    @AfterThrowing(value = "mqPointCut()",throwing = "ex")
    public void afterThrowing(JoinPoint point,Exception ex) throws Throwable {
        Message message= (Message)point.getArgs()[0];
        String messageId = message.getMessageProperties().getMessageId();
        String queue = message.getMessageProperties().getConsumerQueue();
        String exchange = message.getMessageProperties().getReceivedExchange();
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        System.out.println("消息消费异常："+ex.getMessage());
    }

    @After(value = "mqPointCut()")
    public void after(JoinPoint joinPoint){
        System.out.println(joinPoint.getArgs());
        System.out.println("after");
    }

    @AfterReturning(value = "mqPointCut()",returning = "retVal")
    public void afterReturning(JoinPoint point,Object retVal){

        System.out.println("afterReturning ");
    }
}
